import md5 from 'js-md5'
import * as CryptoJS from 'crypto-js'
import store from '../store'
const TokenKey = 'token'

export function getToken() {
  const token = localStorage.getItem(TokenKey)
  return token || ''
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token)
}

export function removeToken() {
  localStorage.removeItem(TokenKey)
  localStorage.removeItem('login_role')
  localStorage.removeItem('school_list')
  localStorage.removeItem('roles')
  return
}

export function getAppId() {
  // '94d575cff463407e92c09d7987928099'
  return process.env.VUE_APP_BASE_API_KEY

}

export function getBaseURL() {
  // 改 .env.development 的 VUE_APP_BASE_API_SD 就可以了 这块不要动了
  // 线上环境改 .env.production 或 .env.staging 的 VUE_APP_BASE_API_SD 根据你的打包指令去更改
  return process.env.VUE_APP_BASE_API
}

export function getWebsocketURL() {
  // 改 .env.development 的 VUE_APP_BASE_API_SD 就可以了 这块不要动了
  // 线上环境改 .env.production 或 .env.staging 的 VUE_APP_BASE_API_SD 根据你的打包指令去更改
  // 上面的appID同理
  return process.env.VUE_APP_BASE_API_WS
}

export function getModelIdByUrl(url) {
  const model_url = store.state.permission.model_url
  for (const i in model_url) {
    if (model_url[i].includes(url)) {
      return i
    }
  }
  return false
}

export function nowTime() {
  return Date.parse(new Date()).toString().substr(0, 10)
}

export function getSign(data) {
  const arr_key = Object.keys(data).sort()
  let str = ''
  arr_key.forEach(i => {
    data[i] = (data[i] == null ? '' : data[i])
    str += i + '=' + data[i]
  })
  return md5(str)
}

export function getAreaCode() {
  const host_name = window.location.hostname
  if (host_name.indexOf('.he.') > 0) {
    return 'he'
  } else if (host_name.indexOf('.sn.') > 0) {
    return 'sn'
  } else if (host_name.indexOf('.sc.') > 0) {
    return 'sc'
  } else if (host_name.indexOf('.sd.') > 0) {
    return 'sd'
  } else {
    return null
  }
}

export function getRandomKey() {
  let str = ''
  const range = 16
  const arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  // 随机产生
  for (let i = 0; i < range; i++) {
    const pos = Math.round(Math.random() * (arr.length - 1))
    str += arr[pos]
  }
  return str
}

export function encrypt(word) {
  const keyStr = process.env.VUE_APP_AES_KEY
  const key = CryptoJS.enc.Utf8.parse(keyStr)
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  return encrypted.toString()
}

export function decrypt(word) {
  const keyStr = process.env.VUE_APP_AES_KEY
  const key = CryptoJS.enc.Utf8.parse(keyStr)
  const decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}
